package com.example.sensors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.os.Handler;

import com.example.singletones.contextObj;

public class Sensor_running_applications extends AbstractSensor {
	public static final String NAME = "RUNNING_APPS";
	public List<String> programs_new;
	public List<String> programs_old;
	private final Handler mHandler = new Handler();

	public Sensor_running_applications() {

	}

	public void disableSensor() {
		super.disableSensor();
		mHandler.removeCallbacks(mUpdateTimeTask);
	}

	private final Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			if (isSensorEnabled) {
				testProgramList();
				mHandler.postDelayed(mUpdateTimeTask, 10000);
			}
		}
	};

	public void testProgramList() {
		programs_new = new ArrayList<String>();
		contextObj.getInstance().getmContext();
		ActivityManager actvityManager = (ActivityManager) contextObj
				.getInstance().getmContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> procInfos = actvityManager
				.getRunningAppProcesses();
		for (int itirator = 0; itirator < procInfos.size(); itirator++) {
			programs_new.add(procInfos.get(itirator).processName);
		}
		List<String> tPrograms_new;
		tPrograms_new = new ArrayList<String>(programs_new);
		List<String> tPrograms_old;
		tPrograms_old = new ArrayList<String>(programs_old);

		tPrograms_old.removeAll(programs_new);
		for (int itirator = 0; itirator < tPrograms_old.size(); itirator++) {
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("processName", tPrograms_old.get(itirator));
			eventArgs.put("processActivity", "OFF");
			OnSensorChanged(eventArgs);
		}

		tPrograms_new.removeAll(programs_old);
		for (int itirator = 0; itirator < tPrograms_new.size(); itirator++) {
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("processName", tPrograms_new.get(itirator));
			eventArgs.put("processActivity", "ON");
			OnSensorChanged(eventArgs);
		}
		programs_old = programs_new;

	}

	public void enableSensor() {
		super.enableSensor();
		programs_new = new ArrayList<String>();
		programs_old = new ArrayList<String>();
		contextObj.getInstance().getmContext();
		// TODO Auto-generated method stub
		ActivityManager actvityManager = (ActivityManager) contextObj
				.getInstance().getmContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> procInfos = actvityManager
				.getRunningAppProcesses();
		for (int itirator = 0; itirator < procInfos.size(); itirator++) {
			programs_old.add(procInfos.get(itirator).processName);
			programs_new.add(procInfos.get(itirator).processName);
			Map<String, Object> eventArgs = initEventArgs();
			eventArgs.put("eventType", "sensorInfoReceived");
			eventArgs.put("processName", procInfos.get(itirator).processName);
			eventArgs.put("processActivity", "ON");
			OnSensorChanged(eventArgs);
		}
		mHandler.postDelayed(mUpdateTimeTask, 10000);
	}

	public String getSensorName() {
		return NAME;
	}
}
